package com.example.weddingapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.lifecycle.AndroidViewModel;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.weddingapp.R;
import com.example.weddingapp.adapter.GiftAdapter;
import com.example.weddingapp.base.BaseActivity;
import com.example.weddingapp.base.BaseObserver;
import com.example.weddingapp.databinding.ActivityDetailBinding;
import com.example.weddingapp.utils.ImageLoaderUtil;
import com.example.weddingapp.view.dialog.BottomSheetDialogWidget;

import java.util.List;

import cn.leancloud.AVFile;
import cn.leancloud.AVObject;
import cn.leancloud.AVQuery;
import cn.leancloud.AVUser;

/**
 * @author DaQiang
 * @Date 2020/5/11 22:12
 * @Desc 互动详情
 */
public class ActivityDetailActivity extends BaseActivity<AndroidViewModel, ActivityDetailBinding> {
    private AVObject detail;
    private GiftAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        showContentView();

        setTitle("活动详情");


    }

    @Override
    protected void initView() {
        super.initView();
        setRightText("更多");
        showRightButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showOperateDialog();
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        String json = getIntent().getStringExtra("json");
        detail = AVUser.parseAVObject(json);
        AVFile image = detail.getAVFile("image");
        if (image != null)
            ImageLoaderUtil.displayNet(this, image.getUrl(), bindingView.ivActivity);

        adapter = new GiftAdapter(this);
        bindingView.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        bindingView.recyclerView.setNestedScrollingEnabled(false);
        bindingView.recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getGifts();
    }

    private void showOperateDialog() {
        AVObject user = detail.getAVObject("user");
        if (user.getObjectId().equals(AVUser.getCurrentUser().getObjectId())) {
            //自己的，能编辑，删除，不能送礼
            final String[] strings = {/*"编辑",*/ "删除"};
            new BottomSheetDialogWidget(this)
                    .setItems(strings)
                    .setTextColor(getResources().getColor(R.color.colorPrimary), strings.length - 1)
                    .setOnItemClickListener(new BottomSheetDialogWidget.ItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            detail.deleteInBackground().subscribe(new BaseObserver<Object>() {
                                @Override
                                public void onNext(Object o) {
                                    super.onNext(o);
                                    showToast("删除成功");
                                    finish();
                                }
                            });
                        }
                    })
                    .show();
        } else {
            //别人的，只能送礼
            final String[] strings = {"送礼"};
            new BottomSheetDialogWidget(this)
                    .setItems(strings)
                    .setOnItemClickListener(new BottomSheetDialogWidget.ItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            songLi();
                        }
                    })
                    .show();
        }

    }

    private void songLi() {
        Intent intent = new Intent(this, GiftActivity.class);
        intent.putExtra("json", getIntent().getStringExtra("json"));
        startActivity(intent);
    }

    private void getGifts() {
        AVQuery<AVObject> query = new AVQuery<>("Order");
        query.whereEqualTo("activity", detail);
        query.whereEqualTo("action", "送礼");
        query.include("fromUser");
        query.include("fromUser.headImg");
        query.findInBackground().subscribe(new BaseObserver<List<AVObject>>() {
            @Override
            public void onNext(List<AVObject> list) {
                adapter.setNewData(list);
            }
        });
    }

}
